<?php


namespace app\common\controller;


use app\common\model\SysDictData;

class DictDataController extends BaseController
{
    protected $model = SysDictData::class;

    public function list()
    {
        $dictName = input('dictName', '');
        $dictStatus = input('dictStatus', '');
        $dictType = input('dictType', '');
        $pageSize = intval(input('pageSize')) ?: DEFAULT_PAGE_LIMIT;
        $pageNo = intval(input('pageNo')) ?: DEFAULT_PAGE_NO;
        $where = [];
        $dictName && $where[] = ['dictName', 'like', "%{$dictName}%"];
        $dictType && $where[] = ['dictType', '=', $dictType];
        $dictStatus && $where[] = ['dictStatus', '=', $dictStatus];

        $count = $this->getModel()
            ->where($where)
            ->count();

        $data = $this->getModel()
            ->where($where)
            ->limit(($pageNo - 1) * $pageSize, $pageSize)
            ->select();
        return $this->success($this->formatPage($count, $data));
    }

    public function add()
    {
        $data = request()->all();
        $this->getModel()->insert($data);
        return $this->success();
    }

    public function update()
    {
        $data = request()->all();
        $this->getModel()->where('dictId', '=', $data['dictId'])->update($data);
        return $this->success();
    }

    public function deleteById($id)
    {
        $this->getModel()->where('dictId', '=', $id)->delete();
        return $this->success();
    }

    public function listDataByType()
    {
        $dictType = input('dictType');

        $data = $this->getModel()->where('dictType', '=', $dictType)->select();
        return $this->success($data);
    }

    public function getById()
    {
        $id = input('id');
        $data = $this->getModel()->where('dictId', '=', $id)->find();
        return $this->success($data);
    }

    public function batchDelete()
    {

    }

    public function updateStatus()
    {

    }

}